package com.cs.contract.util;

import com.cs.contract.service.CsCodeRulesService;
import com.cs.contract.domain.CodeRulesResult;
import com.cs.contract.domain.CsCodeRules;

import java.util.Calendar;

/**
 * @Author：zrx
 * @Package：com.cs.cscontract.util
 * @Project：Ruoyi
 * @name：CodeRuleHelp
 * @Date：2024/11/21 16:45
 * @Filename：CodeRuleHelp
 */
public class CodeRuleHelp {
    public static CodeRulesResult GetCodeRule(CsCodeRules csCodeRules) {
        String maxMantissa = csCodeRules.getMaxMantissa();
        int num = Integer.parseInt(maxMantissa);
        num++;
        String reslut = String.format("%0" + csCodeRules.getSerialNumber() + "d", num);
        return computingTime(csCodeRules, reslut);
    }

    public static CodeRulesResult computingTime(CsCodeRules csCodeRules, String maxNumber) {
        String prefix = csCodeRules.getPrefix();
        Calendar calendar = Calendar.getInstance();
        if (!"".equals(csCodeRules.getYear()) && csCodeRules.getYear() != null) {
            prefix += calendar.get(Calendar.YEAR);
        }
        if (!"".equals(csCodeRules.getMounth()) && csCodeRules.getMounth() != null) {
            prefix += calendar.get(Calendar.MONTH) + 1;
        }
        if (!"".equals(csCodeRules.getDay()) && csCodeRules.getDay() != null) {
            prefix += calendar.get(Calendar.DAY_OF_MONTH);
        }
        if (!"".equals(csCodeRules.getHour()) && csCodeRules.getHour() != null) {
            prefix += calendar.get(Calendar.HOUR_OF_DAY);
        }
        if (!"".equals(csCodeRules.getMinute()) && csCodeRules.getMinute() != null) {
            prefix += calendar.get(Calendar.MINUTE);
        }
        if (!"".equals(csCodeRules.getSecond()) && csCodeRules.getSecond() != null) {
            prefix += calendar.get(Calendar.SECOND);
        }
        prefix += maxNumber;
        CodeRulesResult codeRulesResult = new CodeRulesResult();
        codeRulesResult.setCode(prefix);
        codeRulesResult.setMax(maxNumber);
        return codeRulesResult;
    }

    public static CodeRulesResult createCode(CsCodeRulesService csCodeRulesService, String formName) {
        // 根据目标表单查询当前规则
        CsCodeRules codeRules = csCodeRulesService.getCsCodeRulesByTargetForm(formName);
        // 匹配规则
        CodeRulesResult result = CodeRuleHelp.GetCodeRule(codeRules);
        // 设置最大尾数
        codeRules.setMaxMantissa(result.getMax());
        // 修改
        csCodeRulesService.updateCsCodeRules(codeRules);

        return result;
    }
}
